
  	//<![CDATA[
		var report, marker;
		var latLng = new google.maps.LatLng(48.75,19.80);
		var geocoder = new google.maps.Geocoder();
		var map, iw, drag_area, actual;
		var obj, xpos, ypos;
		var z_index = 0;
		var dummy;
		var lat,lng;
		var pocet = 0;
		var geocoder = new google.maps.Geocoder();

		$(function() {
  $('#save').live('click',function(){

             var latitude = $('#latnew').text(),
                 longitude = $('#lngnew').text(),
                 time_seen = document.getElementById('time_seen').value,
                 address = $('#address').text();

                    var data = 
                    'latitude=' + latitude +
                    '&longitude=' + longitude + 
                    '&time_seen=' + time_seen +
                    '&address=' + address +
                    '&report=' + report;



                    $.ajax({
                        type: 'POST',
                        url: '<?="addreport/insert/"?>',
                        cache: false,
                        data: data,
                        success: function(response){

                          var points = 0;
                          var zaver = ""
                          switch(report){
                            case 'css/images/accident.png':
                            points = 3;
                            zaver = "y";
                            break;
                            case 'css/images/bad_route.png':
                            points = 2;
                            zaver = "y";
                            break;
                            default: 
                            points = 1;
                            zaver = "";
                            break;
                          }


                          updateMarkerStatus('<i style="color:red;font-weight: bold;">Pretiahnite ďalšiu udalosť na mapu.</i>');
                          jAlert( "Ďakujeme za pridanie udalosti.<br /><img src='images/smile.png' style='vertical-align: top;'> Získali ste <b>" + points + " bod"+zaver+"</b>.", "Potvrdenie pridania udalosti" );
                          $('#address').html("ešte ste nič nevybrali");
                          $('#xx').html(response);
                          marker.setMap(null);
                          pocet = 0;
                         // }
                        }
                    });
            return false;
        });
});


		function codeAddress() {
    //In this case it gets the address from an element on the page, but obviously you  could just pass it to the method instead
    var address = document.getElementById("addresss").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
    	if (status == google.maps.GeocoderStatus.OK) {
        //In this case it creates a marker, but you can get the lat and lng from the location.LatLng
        map.setCenter(results[0].geometry.location);
        map.setZoom(15);
        var marker = new google.maps.Marker({
        	map: map, 
        	position: results[0].geometry.location
        });
    } else {
    	alert("Geocode was not successful for the following reason: " + status);
    }
});
}

function updateMarkerAddress(str) {
	document.getElementById('address').innerHTML = str;
}

function DummyOView() {
  // Bind this to the map to access MapCanvasProjection
  this.setMap(map);
  // MapCanvasProjection is only available after draw has been called.
  this.draw = function() {};
}

DummyOView.prototype = new google.maps.OverlayView();


document.onmouseup = function() {

  // Unregister mousemove handler
  document.onmousemove = null;
  if (obj) { obj = null; }
};


function initDrag(e) {
	if (pocet==0){
		if(!e) var e = window.event;

  // Drag image's parent div element
  obj = e.target ? e.target.parentNode : e.srcElement.parentElement;
  if(obj.className != "drag") {
  	if(e.cancelable) e.preventDefault();
  	obj = null;
  	return;
  }

  if (obj) {
  // The currently dragged object always gets the highest z-index
  z_index++;
  obj.style.zIndex = z_index.toString();

  xpos = e.clientX - obj.offsetLeft;
  ypos = e.clientY - obj.offsetTop;

  document.onmousemove = moveObj;
}
return false;
}
}


function moveObj(e) {

	if(obj && obj.className == "drag") {

		if(!e) var e = window.event;
		obj.style.left = e.clientX - xpos + "px";
		obj.style.top = e.clientY - ypos + "px";

		obj.onmouseup = function() {

			var gd = map.getDiv();
			var mLeft = gd.offsetLeft;
			var mTop = gd.offsetTop;

			var mWidth = gd.offsetWidth;
			var mHeight = gd.offsetHeight;

			var areaLeft = drag_area.offsetLeft;
			var areaTop = drag_area.offsetTop;

			var oWidth = obj.offsetWidth;
			var oHeight = obj.offsetHeight;

   // The object's pixel position relative to the document
   var x = obj.offsetLeft + areaLeft + oWidth/2;
   var y = obj.offsetTop + areaTop + oHeight/2;

   // Check if the cursor is inside the map div
   if (x > mLeft && x < (mLeft + mWidth) && y > mTop && y < (mTop + mHeight)) {

    // Difference between the x property of iconAnchor
    // and the middle of the icon width
    var anchorDiff = 1;

    // Find the object's pixel position in the map container
    var g = google.maps;
    var pixelpoint = new g.Point(x - mLeft -anchorDiff, y - mTop + (oHeight/2));

    // Corresponding geo point on the map
    var proj = dummy.getProjection();
    var latlng = proj.fromContainerPixelToLatLng(pixelpoint);

    // Create a corresponding marker on the map
    var src = obj.firstChild.getAttribute("src");
    createDraggedMarker(latlng, src);

    // Create dragged marker anew
    fillMarker();
}
};
}
return false;
}


function fillMarker() {

	var m = document.createElement("div");
	m.style.position = "absolute";
	m.style.width = "32px";
	m.style.height = "37px";

	var left;
	if (obj.id == "m5") {
		left = "0px";
	} else if (obj.id == "m1") {
		left = "150px";
	} else if (obj.id == "m2") {
		left = "250px";
	} else if (obj.id == "m3") {
		left = "350px";
	} else if (obj.id == "m4") {
		left = "450px";
	}
	m.style.left = left;

  // Set the same id and class attributes again
 // m.setAttribute("id", obj.id);
 // m.setAttribute((document.all?"className":"class"), "drag");
 m.id = obj.id;
 m.className = "drag";

  // Append icon
  var img = document.createElement("img");
  img.src = obj.firstChild.getAttribute("src");
  img.style.width = "32px";
  img.style.height = "37px";
  m.appendChild(img);

  drag_area.replaceChild(m, obj);

  // Clear initial object
  obj = null;
}


function highestOrder() {

 /**
 * The currently dragged marker on the map
 * always gets the highest z-index too
 */
 return z_index;
}


function createDraggedMarker(point, src) {

	var g = google.maps;
	var image = new g.MarkerImage(src,
		new g.Size(32, 37),
		new g.Point(0, 0),
		new g.Point(15, 32));

	var shadow = new g.MarkerImage("http://maps.gstatic.com/mapfiles/kml/paddle/A_maps.shadow.png",
		new g.Size(59, 32),
		new g.Point(0, 0),
		new g.Point(15, 32));

	marker = new g.Marker({ 
		position: point, 
		map: map,
		clickable: true,
		draggable: true,
		raiseOnDrag: false,
		icon: image, 
		shadow: shadow, 
		zIndex: highestOrder()
	});

	pocet = 1;

	latLng.lat();
	latLng.lng();

	updateMarkerStatus('Ste spokojný s adresou?<br /><i style="color:red;font-weight: bold;">Ak áno, kliknite na značku na mape</i><br /><i id=\"show_help_add\">Zobraz tipy</i> <span style=\"display:none;\" id=\"help_add\"><b>Tip 1:</b> pravým tlačidlom myši odstránite značku. <b>Tip 2</b>: podržaním ľavého tlačidla na značke na mape ju môžete presunúť. <span style="font-size:10px;cursor:pointer;">(Skryť tipy)</span></span>');
	updateMarkerPosition(marker.getPosition());
	updateMarkerLat(marker.getPosition());
	updateMarkerLng(marker.getPosition());
	geocodePosition(point);

	var form = 

	'<div id="bodyContent">'+
	'<b style="color:black">Pridaj udalosť na uvedenú pozíciu</b>' +
        '<table id="add_" border="0">'+
        '<tr><td>Čas a dátum videnia:</td><td><input type="text" value="<?php echo date("d.m.Y H:i"); ?>" id="time_seen" /></td></tr>'+
        '<tr><td><input type="submit" value="Pridať" id="save"></td></tr>'+
        '</table>'+

        '</div>';

        updateMarkerPosition(marker.getPosition());
        updateMarkerLat(marker.getPosition());
        updateMarkerLng(marker.getPosition());

        var infowindow = new google.maps.InfoWindow({

        	content: form,
        	maxWidth :500,
        	maxHeight: 600

        });

        google.maps.event.addListener(marker, 'click', function() {
        	updateMarkerPosition(marker.getPosition());
        	updateMarkerLat(marker.getPosition());
        	updateMarkerLng(marker.getPosition());
        	infowindow.open(map,this);
        	report = this.icon.url;
        });


        g.event.addListener(marker, "dragstart", function() {
   // Close infowindow when dragging the marker whose infowindow is open
   if (actual == this) iw.close();
   // Increment z_index
   z_index++;
   this.setZIndex(highestOrder());
});

        google.maps.event.addListener(marker, 'dragend', function() {

        	updateMarkerStatus('Presun ukončený, ste spokojný s adresou?<br /><i style="color:red;font-weight: bold;">Ak áno, kliknite na značku na mape</i><br /><i id=\"show_help_add\">Zobraz tipy</i> <span style=\"display:none;\" id=\"help_add\"><b>Tip 1:</b> pravým tlačidlom myši odstránite značku. <b>Tip 2</b>: podržaním ľavého tlačidla na značke na mape ju môžete presunúť.<span style="font-size:10px;cursor:pointer;">(Skryť tipy)</span></span>');
        	updateMarkerPosition(marker.getPosition());
        	updateMarkerLat(marker.getPosition());
        	updateMarkerLng(marker.getPosition());
        	geocodePosition(marker.getPosition());

        });


        google.maps.event.addListener(marker, "rightclick", function() {
        	this.setMap(null);
        	pocet = 0;
        	updateMarkerStatus('<i style="color:red;font-weight: bold;">Pretiahnite udalosť na mapu.</i>');
        	$('#address').html("ešte ste nič nevybrali");
        });

    }

    function updateMarkerStatus(str) {
    	document.getElementById('markerStatus').innerHTML = str;
    }

    function updateMarkerPosition(latLng) {
    	document.getElementById('info').innerHTML = [
    	latLng.lat(),
    	latLng.lng()
    	].join(', ');
    }

    function updateMarkerLat(latLng) {
    	document.getElementById('latnew').innerHTML = [
    	latLng.lat()
    	];
    }

    function updateMarkerLng(latLng) {
    	document.getElementById('lngnew').innerHTML = [
    	latLng.lng()
    	];
    }

    function geocodePosition(pos) {
    	geocoder.geocode({
    		latLng: pos
    	}, function(responses) {
    		if (responses && responses.length > 0) {
    			updateMarkerAddress(responses[0].formatted_address);
    		} else {
    			updateMarkerAddress('Cannot determine address at this location.');
    		}
    	});
    }

    function buildMap() {
    	var g = google.maps;
    	var opts_map = {
    		center: new g.LatLng(48.75,19.80),
    		zoom: 7,
    		mapTypeId: g.MapTypeId.ROADMAP,
    		streetViewControl: false,

  // mapTypeControlOptions: {
  //   mapTypeIds: [ g.MapTypeId.ROADMAP, g.MapTypeId.SATELLITE, g.MapTypeId.TERRAIN]
  // },
  panControl: false,
  zoomControlOptions: {
  	style: g.ZoomControlStyle.SMALL
  }};

  map = new g.Map(document.getElementById("map"), opts_map);
  iw = new g.InfoWindow();

  // v2 behaviour
  g.event.addListener(map, "click", function() {
  	if (iw) iw.close();
  });

  // Register mousedown listener
  drag_area = document.getElementById("markers");
  drag_area.onmousedown = initDrag;


  // Add a dummy overlay for later use.
  // Needed for API v3 to convert pixels to latlng.
  dummy = new DummyOView();



  var input = document.getElementById('address_auto');
  var autocomplete = new google.maps.places.Autocomplete(input);

  autocomplete.bindTo('bounds', map);


 // var infowindow = new google.maps.InfoWindow();
 //        var marker = new google.maps.Marker({
 //          map: map
 //        });

google.maps.event.addListener(autocomplete, 'place_changed', function() {
          // infowindow.close();
          var place = autocomplete.getPlace();
          if (place.geometry.viewport) {
          	map.fitBounds(place.geometry.viewport);
          	map.setZoom(11);
          } else {
          	map.setCenter(place.geometry.location);
            map.setZoom(15);  // Why 17? Because it looks good.
        }

          // var image = new google.maps.MarkerImage(
          //     place.icon,
          //     new google.maps.Size(71, 71),
          //     new google.maps.Point(0, 0),
          //     new google.maps.Point(17, 34),
          //     new google.maps.Size(35, 35));
          // marker.setIcon(image);
          // marker.setPosition(place.geometry.location);

          var address = '';
          if (place.address_components) {
          	address = [
          	(place.address_components[0] && place.address_components[0].short_name || ''),
          	(place.address_components[1] && place.address_components[1].short_name || ''),
          	(place.address_components[2] && place.address_components[2].short_name || '')
          	].join(' ');
          }

          // infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
          // infowindow.open(map, marker);
      });

        // Sets a listener on a radio button to change the filter type on Places
        // Autocomplete.
        function setupClickListener(id, types) {
        	var radioButton = document.getElementById(id);
        	google.maps.event.addDomListener(radioButton, 'click', function() {
        		autocomplete.setTypes(types);
        	});
        }

        setupClickListener('changetype-all', []);
        setupClickListener('changetype-establishment', ['establishment']);
        setupClickListener('changetype-geocode', ['geocode']);


    }

    window.onload = buildMap;
